package client.gui.panels;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import controllers.ControllerIF;

public class Login extends JPanel implements PanelIF, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Instance of the panel's controller
	 */
	private ControllerIF panelController;
	
	private JTextField usernameTextField;
	private JPasswordField passwordField;
	private JButton clearButton;
	private JButton exitButton;
	private JButton loginButton;
	

	/**
	 * Create the frame.
	 */
	public Login() {
		initGui();
	}
	
	/**
	 * Initialize the user interface
	 */
	public void initGui() {
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		add(Box.createVerticalStrut(15));
		
		// Ad the login title
		JPanel titlePanel = new JPanel();
		add(titlePanel);
		titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.X_AXIS));		
		titlePanel.add(Box.createHorizontalGlue());		
		JLabel lblLogo = new JLabel("Login");
		lblLogo.setFont(new Font("Tahoma", Font.PLAIN, 24));
		titlePanel.add(lblLogo);		
		titlePanel.add(Box.createHorizontalGlue());
		
		add(Box.createVerticalStrut(20));
		
		// content panel holds both text fields
		JPanel contentPanel = new JPanel();
		add(contentPanel);
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));

		// username panel
		JPanel usernamePanel = new JPanel();
		contentPanel.add(usernamePanel);
		usernamePanel.setLayout(new BoxLayout(usernamePanel, BoxLayout.X_AXIS));		
		usernamePanel.add(Box.createHorizontalStrut(20));		
		JLabel lblUsername = new JLabel("Username:");
		usernamePanel.add(lblUsername);
		usernamePanel.add(Box.createHorizontalStrut(20));
		usernameTextField = new JTextField();
		usernameTextField.setColumns(40);
		usernameTextField.setMaximumSize(usernameTextField.getPreferredSize());
		usernamePanel.add(usernameTextField);

		contentPanel.add(Box.createVerticalStrut(15));
		
		// password panel
		JPanel passwordPanel = new JPanel();
		contentPanel.add(passwordPanel);
		passwordPanel.setLayout(new BoxLayout(passwordPanel, BoxLayout.X_AXIS));
		passwordPanel.add(Box.createHorizontalStrut(20));		
		JLabel lblPassword = new JLabel("Password:");
		passwordPanel.add(lblPassword);
		passwordPanel.add(Box.createHorizontalStrut(20));
		passwordField = new JPasswordField();
		passwordField.setColumns(40);
		passwordField.setMaximumSize(passwordField.getPreferredSize());
		passwordPanel.add(passwordField);

		contentPanel.add(Box.createVerticalStrut(15));
		
		// Buttons panel
		JPanel buttonPanel = new JPanel();
		contentPanel.add(buttonPanel);
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.add(Box.createHorizontalGlue());		
		
		exitButton = new JButton("Exit");
		exitButton.addActionListener(this);
		buttonPanel.add(exitButton);
		
		buttonPanel.add(Box.createHorizontalStrut(20));
		
		clearButton = new JButton("Clear");
		clearButton.addActionListener(this);
		buttonPanel.add(clearButton);

		buttonPanel.add(Box.createHorizontalStrut(20));
		
		loginButton = new JButton("Login");
		loginButton.addActionListener(this);
		buttonPanel.add(loginButton);
		
		buttonPanel.add(Box.createHorizontalGlue());
		
		contentPanel.add(Box.createVerticalStrut(15));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(exitButton)) {
			System.exit(0);
		}
		else if(e.getSource().equals(clearButton)) {
			usernameTextField.setText("");
			passwordField.setText("");
		}
		else if(e.getSource().equals(loginButton)) {
			panelController.action();
		}
	}

	/**
	 * set local parameter to hold the controller reference
	 * @param cif Controller interface
	 */
	public void setController(ControllerIF cif) {
		panelController = cif;		
	}
	
	/**
	 * get the username as inserted by the user
	 * @return String
	 */
	public String getUsername() {
		return usernameTextField.getText();
	}
	
	public void setUsername(String username) {
		usernameTextField.setText(username);
	}

	/**
	 * get the password as inserted by the user
	 * @return String
	 */
	@SuppressWarnings("deprecation")
	public String getPassword() {
		return passwordField.getText();
	}
	
	public void setPassword(String pass) {
		passwordField.setText(pass);
	}

}
